from strategy.StrategyTemplate import StrategyTemplate
from utils.jsl_data_map import default_columns
# from database.lhjy import engine
from strategy import strategy_filter
# from utils.simple_utls import code_fix
import pandas as pd


class Dao1(StrategyTemplate):
    """
    道哥转债池1:
        低价潜在活跃债
    """
    def __init__(self):
        super(Dao1, self).__init__()
        self.strategy_name = "低价潜在活跃债策略"
        self.strategy_descrption = "120元以下潜在活跃债"

    def run(self):
        self.data = strategy_filter.filter_base(self.data)

        self.data = self.data.loc[(self.data['现价'] > 90) & (self.data['现价'] < 120)]
        filter_list = []
        for code in self.data['转债代码']:
            code_with_pre = code_fix(code)
            try:
                if code_with_pre is not None:
                    code_table = pd.read_sql_table(code_with_pre, con=engine)
                    latest_table = code_table.tail(10)
                    count = latest_table['turnover_rt'][latest_table['turnover_rt'].astype('float') > 15].count()
                    if count >= 5:
                        filter_list.append(code_with_pre[2:])
            except ValueError as e:
                pass
        self.data = self.data[self.data['转债代码'].isin(filter_list)]
        self.data = self.data.sort_values(by='剩余规模', ascending=True)
        # 获得默认列
        self.data = self.data[default_columns]
        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)


if __name__ == '__main__':
    ins = Dao1()
    ins.topN = 20
    ins.test()